package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.OrderVo.UcenterMemberOrder;
import com.atguigu.commonutils.R;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ct_start
 * @since 2021-08-20
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping ("login")
    public R loginUser(@RequestBody UcenterMember member){
        // 调用service 方法进行登录
        // 返回token 用于 jwt 的实现
        String token=ucenterMemberService.login(member);
        return  R.ok().data("token",token);

    }

    // 注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    // 根据token 字符串获取用户信息

    // 由于登录之后会返回一串token 字符串,我们需要解析token字符串里面的值进行登录用户属性的显示
    // 这种request 请求怎么搞  swagger直接是可以什么都不用填就发送这个请求
    @GetMapping("getMemberInfo")

    public R getMemberInfo(HttpServletRequest request){

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        // 调用数据库,根据用户id查询,获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return  R.ok().data("userInfo",member);
    }

    //根据token字符串获取用户信息
    @PostMapping("getInfoUc/{id}")
    public R getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
       UcenterMember memeber = new UcenterMember();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return R.ok().data("member",memeber);
    }

    // 根据用户id 获取到用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember byId = ucenterMemberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder=new UcenterMemberOrder();
        BeanUtils.copyProperties(byId,ucenterMemberOrder);

        return  ucenterMemberOrder;
    }



    // 查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){

        Integer count=ucenterMemberService.countRegisterDay(day);
        return  R.ok().data("countRegister",count);
    }

}

